%% Read Model and Compute Steady State and Solution
% by Jaromir Benes
%
% In this file, we create two versions of the same model. One version will
% be parameterized so that the variable `B` (net assets) will be positive
% whereas the other version will have `B` negative. In both models, `B`
% will be a log variable.
%
% Models with negative log variables can be thought of as identical models
% in which we replace the negative log variable with its

%% Clear Workspace

clear;
close all;
clc;
irisrequired 20140813;

%% Load Model File and Create Model Object

m = model('log_minus.model');

%% Parameterize Two Different Models
%
% Create two model objects, `m1` and `m2`. Parameterize the first model
% <?positiveB?> so that the steady-state level of net assets, `B`, is
% positive. This requires that `alp/bet` (which determines the domestic
% steady-state rate of interest) be smaller than the world rate of
% interest, `Rw`. Parameterize the second model <?negativeB?> so that `B`
% is negative in steady state. The values for `bet` are chosen so that the
% absolute value of the debt-to-output ratio, `B/Y`, is the same in each
% model.

m.alp = 1.02; % Gross rate of growth.
m.zet = 0.1; % Sensitivity of risk premium to real debt.
m.Rw = 1.05; % Gross rate of interest in the rest of the world.

m1 = m; %?positiveB?
m1.bet = 1.02/(1.05/1.02); % Discount parameter approx 0.9909.

m2 = m; %?negativeB?
m2.bet = 1.02/(1.05*1.02); % Discount parameter approx 0.9524.

%% Find Steady State
%
% Call the function `sstate` to find the steady state for each model.
% Pretend we do not know the sign of net assets, `B`, in steady state. Use
% the option `'unlog=' true` to temporarily treat the variable `B` as
% non-log variable, allowing thus for either positive or negative steady
% state.
%
% Furthermore, use the option `'fixLevel='` <?fixLevel?> to fix the
% steady-state level of `Y` to `1` <?one?> so that it is the same in both
% models. The variable `Y` (real output) is growing in steady state, and
% hence we can pick any point on the balanced-growth path (if we did not
% fix its level, everything would work just fine -- only that `Y` would
% then depend on the particular numerical procedure used to evaluate the
% steady state).

m1.Y = 1; %?one?
m1 = sstate(m1,'Growth=',true,'unlog=','B','fixLevel=','Y'); %?fixLevel?
% m1 = sstate(m1,'Growth=',true);
chksstate(m1);

m2.Y = 1; %?one?
m2 = sstate(m2,'Growth=',true,'unlog=','B','fixLevel=','Y'); %?fixLevel?
% m2 = sstate(m2,'Growth=',true,'LogMinus=','B');
chksstate(m2);

%% Compare Steady States of the Two Models
%
% Display a steady-state database (with real numbers indicating the level,
% and imaginary numbers indicating the rate of growth) for each model. Note
% that `B/Y` in model `m1` equals `-B/Y` in model `m2` (this is a feature
% achieved by calibrating the parameter `bet` in the above way).

get(m1,'sstate')
get(m2,'sstate')

%% Log Variables Can Never Be Zero
%
% Assign the parameter `bet` a value implying zero net asset position, i.e.
% `B` < 0. This requires that `R` be the same as `Rw` in steady state, or
% `bet = alp/Rw`. Then try to find the steady state of such a model, using
% the option `'unlog=`'. The function `sstate` returns a correct set of
% steady-state values but at the same time throws a warning that log
% variables cannot be zero. Models in which some log variables have zero
% steady state cannot be solved or simulated.

m3 = m;
m3.bet = m3.alp/m3.Rw;

m3.Y = 1;
m3 = sstate(m3,'growth=',true,'unlog=','B','fixLevel=','Y');
get(m3,'sstate')

%% Calculate First-Order Solution
%
% Use the function `solve` to compute first-order solutions for the two
% model objects, `m1` and `m2`. The first-order solutions will be used to
% simulate the models in another file.

m1 = solve(m1);
m2 = solve(m2);

%% Save Model Objects for Further Use
%
% Save the two solved models to a (binary) mat file.

save read_model.mat m1 m2;

%% Help on IRIS Functions Used in This File
%
% Use either `help` to display help in the command window, or `idoc`
% to display help in an HTML browser window.
%
%    help model/model
%    help model/sstate
%    help model/chksstate
%    help model/subsasgn
%    help model/subref
%    help model/get
%    help model/solve
